//
//  WZScheduleRemindController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/6/16.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZScheduleRemindController.h"
#import "LocationNotification.h"

#define LocalNotification_ScheduleTitle_KEY @"scheduleTitle"
#define LocalNotification_ScheduleStartTime_KEY @"scheduleStartTime"
#define LocalNotification_ScheduleStartDate_KEY @"scheduleStartDate"

NSString *const WZScheduRemindOfLocalNoticfication = @"WZScheduRemindOfLocalNoticfication";

@interface WZScheduleRemindController ()

@property (nonatomic, assign) NSInteger badgeNumber;
@property (nonatomic, copy) NSString *lnName;
@property (weak, nonatomic) IBOutlet UILabel *remindTime;
@property (weak, nonatomic) IBOutlet UILabel *scheduleTitle;

@end

@implementation WZScheduleRemindController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localNotice:) name:WZScheduRemindOfLocalNoticfication object:nil];
    
    [self setupUI];
}

- (void)setupUI {
    
    NSString *title = [self.ln.userInfo objectForKey:LocalNotification_ScheduleTitle_KEY];
    self.scheduleTitle.text = title;
    NSString *startTime = [self.ln.userInfo objectForKey:LocalNotification_ScheduleStartTime_KEY];
    self.remindTime.text = [NSString stringWithFormat:@"今天:%@", startTime];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (IBAction)remindLaterBtnDidClick:(id)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"推迟5分钟" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self postPoneRemindWithSinceNowWithMinutes:10];
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"推迟10分钟" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self postPoneRemindWithSinceNowWithMinutes:10];
        
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"推迟15分钟" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self postPoneRemindWithSinceNowWithMinutes:15];
    }];
    
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"推迟1小时" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self postPoneRemindWithSinceNowWithMinutes:60];
    }];
    
    UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"推迟1天" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self postPoneRemindWithSinceNowWithMinutes:24 * 60];
    }];
    
    UIAlertAction *action6 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
//        [self dismissViewControllerAnimated:YES completion:^{
//            
            WZLog(@"取消推迟");
//        }];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [alert addAction:action4];
    [alert addAction:action5];
    [alert addAction:action6];
    
    //    alert.actions = @[action1, action2, action3];
    
    [self presentViewController:alert animated:YES completion:nil];

    
}

- (IBAction)closeBtnDidClick:(id)sender {
    
    NSArray *lns = [UIApplication sharedApplication].scheduledLocalNotifications;
    if (lns.count > 0 && _ln) {
        
        for (UILocalNotification *ln in lns) {
            WZLog(@"通知名称是:%@",ln.userInfo[LocalNotification_ScheduleTitle_KEY]);
            NSString *lnName = ln.userInfo[LocalNotification_ScheduleTitle_KEY];
            if ([lnName isEqualToString:_ln.userInfo[LocalNotification_ScheduleTitle_KEY]]) {
                
                [[UIApplication sharedApplication] cancelLocalNotification:ln];
                WZLog(@"提醒被关闭了");
                _ln = nil;
                break;
            }
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//- (void)localNotice:(NSNotification *)notice {
//    _lnName = nil;
//    _ln = nil;
//    NSString *noticeName = notice.userInfo[LocalNotification_Userinfo_KEY];
//    _lnName = noticeName;
//    _ln = notice.userInfo[LocalNotification_detail];
//    WZLog(@"noticeValue = %@", noticeName);
//}


- (void)postPoneRemindWithSinceNowWithMinutes:(NSInteger)mintues {
    
    NSArray *lns = [UIApplication sharedApplication].scheduledLocalNotifications;
    if (lns.count > 0 && _ln) {
        
        for (UILocalNotification *ln in lns) {
            WZLog(@"通知名称是:%@",ln.userInfo[LocalNotification_ScheduleTitle_KEY]);
            NSString *lnName = ln.userInfo[LocalNotification_ScheduleTitle_KEY];
            if ([lnName isEqualToString:_ln.userInfo[LocalNotification_ScheduleTitle_KEY]]) {
                
                [[UIApplication sharedApplication] cancelLocalNotification:ln];
                
                NSTimeInterval postPoneTime = mintues * 60;
                NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:postPoneTime];
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                [dict setObject:_ln.userInfo[LocalNotification_ScheduleTitle_KEY] forKey:LocalNotification_ScheduleTitle_KEY];
                [dict setObject:_ln.userInfo[LocalNotification_ScheduleStartTime_KEY] forKey:LocalNotification_ScheduleTitle_KEY];
                [dict setObject:_ln.userInfo[LocalNotification_ScheduleStartDate_KEY] forKey:LocalNotification_ScheduleStartDate_KEY];
                
                [LocationNotification registerLocalNotificationWithFireDate:fireDate repeatType:NSCalendarUnitMonth entity:dict.copy];
                break;
            }
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
    
        
    }];
}


@end
